//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Forward.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/InterestRate.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CForward::CForward (boost::shared_ptr<QuantLib::Forward>& childNative, Object^ owner) : CInstrument(CForward::typeid)
{
#ifdef HANDLE
	_phForward = NULL;
#endif
	_ppForward = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppForward));
}
Cephei::QL::Instruments::CForward::CForward (QuantLib::Forward& childNative, Object^ owner) : CInstrument(CForward::typeid)
{
#ifdef HANDLE
	_phForward = NULL;
#endif
	_ppForward = new boost::shared_ptr<QuantLib::Forward> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppForward));
    _ForwardOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Instruments::CForward::CForward (CForward^ copy) : CInstrument(CForward::typeid)
{
#ifdef HANDLE
	_phForward = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppForward = new boost::shared_ptr<QuantLib::Forward> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppForward));
    }
}
Cephei::QL::Instruments::CForward::CForward (PLATFORM::Type^ t) : CInstrument(CForward::typeid)
{
#ifdef HANDLE
	_phForward = NULL;
#endif
	if (!t->IsSubclassOf(CForward::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CForward::CForward (QuantLib::Handle<QuantLib::Forward>& childNative, Object^ owner)  : CInstrument(CForward::typeid)
{
	_phForward = &childNative;
	_ppForward = &static_cast<boost::shared_ptr<QuantLib::Forward>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppForward));
    _ForwardOwner = owner;
}
Cephei::QL::Instruments::CForward::CForward (QuantLib::Handle<QuantLib::Forward> childNative)  : CInstrument(CForward::typeid)
{
	_phForward = &childNative;
	_ppForward = &static_cast<boost::shared_ptr<QuantLib::Forward>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppForward));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CForward::CForward (QuantLib::Forward childNative)  : CInstrument(CForward::typeid)
{
#ifdef HANDLE
	_phForward = NULL;
#endif
	_ppForward = new boost::shared_ptr<QuantLib::Forward> (new QuantLib::Forward (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppForward));
}
#endif

Cephei::QL::Instruments::CForward::~CForward ()
{
    if (_ppForward != NULL)
    {
	    delete _ppForward;
        _ppForward = NULL;
    }
}
Cephei::QL::Instruments::CForward::!CForward ()
{
    if (_ppForward != NULL)
    {
	    delete _ppForward;
    }
}
QuantLib::Forward& Cephei::QL::Instruments::CForward::GetReference ()
{
    if (_ppForward == NULL) throw REFNEW NativeNullException ();
	return **_ppForward;
}
boost::shared_ptr<QuantLib::Forward>& Cephei::QL::Instruments::CForward::GetShared ()
{
    if (_ppForward == NULL) throw REFNEW NativeNullException ();
	return *_ppForward;
}
QuantLib::Forward* Cephei::QL::Instruments::CForward::GetPointer ()
{
    if (_ppForward == NULL) throw REFNEW NativeNullException ();
	return &**_ppForward;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Forward>& Cephei::QL::Instruments::CForward::GetHandle ()
{
	if (_phForward == NULL)
	{
		_phForward = new Handle<QuantLib::Forward> (*_ppForward);
	}
	return *_phForward;
}
#endif
bool Cephei::QL::Instruments::CForward::HasNative () 
{
	return (_ppForward != NULL);
}

QL::Times::BusinessDayConventionEnum Cephei::QL::Instruments::CForward::BusinessDayConvention::get ()
{
    try
    {
    	QuantLib::BusinessDayConvention _rv = (QuantLib::BusinessDayConvention)(*_ppForward)->businessDayConvention ( );   
        QL::Times::BusinessDayConventionEnum _nrv = (QL::Times::BusinessDayConventionEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::ICalendar^ Cephei::QL::Instruments::CForward::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar& _rv = (QuantLib::Calendar&)(*_ppForward)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = REFNEW Cephei::QL::Times::CCalendar (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Instruments::CForward::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter& _rv = (QuantLib::DayCounter&)(*_ppForward)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Instruments::CForward::DiscountCurve::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure> _rv = (Handle<QuantLib::YieldTermStructure>)(*_ppForward)->discountCurve ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = REFNEW Cephei::QL::Termstructures::CYieldTermStructure (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CForward::ForwardValue::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppForward)->forwardValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IInterestRate^ Cephei::QL::Instruments::CForward::ImpliedYield (Double underlyingSpotValue, Double forwardValue, DateTime settlementDate, QL::CompoundingEnum compoundingConvention, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _underlyingSpotValue = (QuantLib::Real)ValueHelper::Convert (underlyingSpotValue); //a
        QuantLib::Real _forwardValue = (QuantLib::Real)ValueHelper::Convert (forwardValue); //a
        QuantLib::Date _settlementDate = (QuantLib::Date)ValueHelper::Convert (settlementDate); //a
        QuantLib::Compounding _compoundingConvention = (QuantLib::Compounding)compoundingConvention ;
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter _dayCounter = static_cast<QuantLib::DayCounter> (_CdayCounter->GetReference ()); 
    	QuantLib::InterestRate _rv = (QuantLib::InterestRate)(*_ppForward)->impliedYield ( _underlyingSpotValue,  _forwardValue,  _settlementDate,  _compoundingConvention,  _dayCounter );   
        Cephei::QL::CInterestRate^ _nrv = REFNEW Cephei::QL::CInterestRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Instruments::CForward::IncomeDiscountCurve::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure> _rv = (Handle<QuantLib::YieldTermStructure>)(*_ppForward)->incomeDiscountCurve ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = REFNEW Cephei::QL::Termstructures::CYieldTermStructure (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Instruments::CForward::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppForward)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CForward::SettlementDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppForward)->settlementDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CForward::SpotIncome (Cephei::QL::Termstructures::IYieldTermStructure^ incomeDiscountCurve)
{
    CYieldTermStructure^ _CincomeDiscountCurve;
    try
    {
        _CincomeDiscountCurve = safe_cast<CYieldTermStructure^> (incomeDiscountCurve);
        _CincomeDiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _incomeDiscountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CincomeDiscountCurve->GetHandle ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppForward)->spotIncome ( _incomeDiscountCurve );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CincomeDiscountCurve != nullptr) _CincomeDiscountCurve->Unlock();
    }
}
Double Cephei::QL::Instruments::CForward::SpotValue::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppForward)->spotValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

